package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UtilContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;


    @RequiredPermission(name="投票页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        Long eid = UtilContext.getCurrentUser().getId();

        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="投票删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
            model.addAttribute("activityItems",activityItemService.selectByAid(id));
        }
        return "activity/input";
    }

    @RequiredPermission(name="投票",expression="activity:resultShow")
    @RequestMapping("/resultShow")
    public String resultShow(Activity activity,Long radioId) {
        activityItemService.update(radioId);
        activityService.saveRelation(radioId,activity.getId());

        return "redirect:/activity/result?id="+activity.getId();
    }

  @RequiredPermission(name="投票结果显示",expression="activity:result")
    @RequestMapping("/result")
    public String result(Model model,Long id) {
      Activity activity = activityService.get(id);
      //去数据库查对应的数据给结果页面显示
        model.addAttribute("activity",activity);
        //去数据库查对应的选项给结果页面显示
        List<ActivityItem> activityItems = activityItemService.selectByAid(id);
        model.addAttribute("activityItems",activityItems);
        //model.addAttribute("totalNum",totalNum);

        return "activity/result";
    }




}
